/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.logging {
	import org.splink.library.utils.ArrayUtils;

	/**
	 * Provides an API to retrieve <code>ILogger</code> implementing instances. Usually you want to 
	 * add one or more <code>ILoggerFactory</code> instances at application startup and get an 
	 * <code>ILogger</code> for every class you want to log from.
	 * 
	 * @author Max Kugland
	 */
	public class LoggerProvider {
		private static const NULL_LOGGER : ILogger = new NullLogger();		private static var _logfactories : Array = new Array();

		/**
		 * Adds an <code>ILoggerFactory</code>. Via <code>getLogger</code> you can retrieve 
		 * <code>ILogger</code> implementing instances from different factories.
		 * 
		 * @param logger the <code>ILoggerFactory</code> to add
		 * @see ILoggerFactory
		 */
		public static function addLoggerFactory(factory : ILoggerFactory) : void {
			if(_logfactories.indexOf(factory) == -1) _logfactories.push(factory);
		}

		/**
		 * Removes an <code>ILoggerFactory</code>.
		 * 
		 * @param logger the <code>ILoggerFactory</code> to remove
		 * @see ILoggerFactory
		 */
		public static function removeLoggerFactory(factory : ILoggerFactory) : void {
			ArrayUtils.remove(_logfactories, factory);
		}

		/**
		 * Returns an <code>ILogger</code> using the given factory for the given class. 
		 * 
		 * @param factoryId the id of the <code>ILoggerFactory</code> which is used to create the
		 * <code>ILogger</code>
		 * @param clazz the class in which the <code>ILogger</code> will be used  
		 * @return an <code>ILogger</code> implementing instance
		 */
		public static function getLogger(factoryId : String, clazz : Class) : ILogger {
			for each (var factory : ILoggerFactory in _logfactories) {
				if(factory.id == factoryId) {
					return factory.getLogger(clazz);
				}
			}
			return NULL_LOGGER;
		}
	}
}
